package hsintiao.com.xintiaoAPP.controller;


import hsintiao.com.xintiaoAPP.entity.Article;
import hsintiao.com.xintiaoAPP.entity.ArticleType;
import hsintiao.com.xintiaoAPP.entity.vo.ArticlePageVO;
import hsintiao.com.xintiaoAPP.entity.vo.ArticleVO;
import hsintiao.com.xintiaoAPP.service.ArticleService;
import hsintiao.com.xintiaoAPP.util.AjaxResult;
import hsintiao.com.xintiaoAPP.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;


/*
* 文章中心
* */
@RestController
@RequestMapping("/article")
@CrossOrigin
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    /*
    * 获取所有文章(暂不使用)
    * */
    @PostMapping("/getAll")
    @ResponseBody
    public AjaxResult getAll(){
        try {
            AjaxResult ajaxResult = articleService.allArticle();
            return ajaxResult;
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setCode(201).setSuccess(false).setMessage("查询失败"+e.getMessage());
        }
    }

    /*
    * 分页展示对应类型的文章数据(默认推荐类型)
    * */
    @PostMapping("/page")
    @ResponseBody
    public AjaxResult getAllByTypeForPage(@RequestBody ArticlePageVO pageVO){
        //System.out.println("类型="+pageVO);
        AjaxResult ajaxResult = articleService.getAllByTypeForPage(pageVO);
        return ajaxResult;
    }

    /*
    * 从redis中获取所有(暂不使用)
    * */
    @PostMapping("/getAllForRedis")
    @ResponseBody
    public AjaxResult getgetAllForRedis(@RequestBody ArticlePageVO pageVO){
        try {
            String key="article:"+pageVO.getTypeName();
            byte[] bytes = RedisUtils.INSTANCE.get(key.getBytes());
            //将byte数据反序列出对象
            ByteArrayInputStream bi = new ByteArrayInputStream(bytes);

            ObjectInputStream oi = new ObjectInputStream(bi);

            //从对象读取流中读取出p对象
            ArticleVO vo = (ArticleVO) oi.readObject();
            System.out.println("redis的对象="+vo);
            return AjaxResult.me().setResultObj(vo);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setCode(201).setSuccess(false).setMessage("添加"+e.getMessage());
        }
    }

    /*
    * 设置所有文章到redis中(暂不使用)
    * */
    @PostMapping("/setAllForRedis")
    @ResponseBody
    public AjaxResult setAllForRedis(@RequestBody ArticlePageVO pageVO){
        try {
            AjaxResult ajaxResult = articleService.getAllByTypeForPage(pageVO);
            ArticleVO vo= (ArticleVO) ajaxResult.getResultObj();
            //将对象序列化成字节数组
            ByteArrayOutputStream ba = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(ba);
            //用对象序列化流来将p对象序列化，然后把序列化之后的二进制数据写到ba流中
            oos.writeObject(vo);
            //将ba流转成byte数组
            byte[] pBytes = ba.toByteArray();
            String key="article:"+pageVO.getTypeName();

            RedisUtils.INSTANCE.set(key.getBytes(),pBytes);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setCode(201).setSuccess(false).setMessage("添加"+e.getMessage());
        }
    }

    /*
    * 获取所有的文章类型
    * */
    @PostMapping("/getAllByType")
    @ResponseBody
    public AjaxResult getAllByType(@RequestBody ArticleType articleType) {

        try {
            Long typeId = articleType.getId();
            AjaxResult result = articleService.getArticleByType(typeId);
            return result;
            //return AjaxResult.me().setCode(200).setSuccess(true).setMessage("成功").setResultObj(allArticles);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setCode(201).setSuccess(false).setMessage("查询失败");
        }
    }

    /*
    * 根据选择的文章进入详情页
    * */
    @PostMapping("/getById")
    @ResponseBody
    public AjaxResult getById(@RequestBody Article article) {
        try {
            Long id = article.getId();
            AjaxResult result = articleService.getOneById(id);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setCode(201).setSuccess(false).setMessage("查询失败");
        }
    }

    /*
    * 搜索文章(关键字模糊查询)
    * */
    @PostMapping("/search")
    @ResponseBody
    public AjaxResult searchByKeyword(@RequestBody ArticleVO vo){
        try {
            String keyword = vo.getKeyword();
            System.out.println("查询关键字="+keyword);
            return articleService.searchByKeyword(keyword);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setCode(201).setSuccess(false).setMessage("查询失败");
        }
    }


}
